<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mproduct extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function getListProducts($number,$offset)
    {
        $where = '';
        $title = 'Tất cả';
        $id = $this->getParamUrl(3);
        //var_dump($id);
        $limit = $offset ? intval($offset) : '0';
        
        if($id != 28)
        {
            $title = $this->db->query('SELECT title FROM categories WHERE id = '.$id)->row_array();
            $title = $title['title'];
            
            $id_c = $this->checkChild($id);
            //var_dump($id_c.'    fuck');
            
            $where = " AND id_c IN ({$id_c}) ";
        }
        
        //die($where.'adfadsf');
        
        $sql = "SELECT * FROM product WHERE 1=1 {$where} ";
        $sql .= " ORDER BY id ASC";
        //echo $sql;
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('title' => $title,'data' => $xs, 'count' => $count);
    }

    public function getListProductsHomepage($number,$offset)
    {
        $limit = $offset ? intval($offset) : '0';
        

        $sql = "SELECT * FROM product";
        $sql .= " ORDER BY id ASC";
        
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);
    }
    
    public function getListProductsSearch()
    {
        $where = '';
        
        $search = $this->getParamString('search','');
        if($search)
        {
            $where .= " AND ( p.title LIKE '%$search%' OR p.title_ascii LIKE '%$search%' OR p.title_seo LIKE '%$search%'  OR p.title_seo_ascii LIKE '%$search%'  OR p.price LIKE '%$search%' OR p.description_seo LIKE '%$search%' ) ";
        }
        
        $arr_prod = $this->db->query("SELECT p.*, l.link FROM product p INNER JOIN link_all l ON p.n_link = l.n_link WHERE 1=1 {$where} ")->result_object();
        //var_dump($arr_prod);
        return $arr_prod;
    }
    
    private function checkChild($id)
    {
        $d = $this->db->query('SELECT id FROM categories WHERE parent IN ('.$id.')');
        
        $check = $d->num_rows();
        $ds = $d->row_object();
        if($check > 0)
        {
            $dt = $this->db->query('SELECT id FROM categories WHERE parent = '.$id)->result_object();
            $val = '';
            foreach($dt AS $el=>$le)
            {
                $val .= ','.$le->id;
            }
            
            $str = substr($val,1);
            return Mproduct::checkChild($str);
            
        }else{
            $dcv = $id;
            return $dcv;
        }
    }
    
    public function getListProductsBNKM($type = '', $limit = '')
    {
        $lm = '';
        $where = '';
        
        if($type == 'bn')
        {
            $where .= " AND p.ban_nhieu = 1 ";
        }
                
        if($type == 'km')
        {
            $where .= " AND p.khuyenmai = 1 ";
        }
        
        if($limit)
        {
            $lm = " LIMIT {$limit} ";
        }
        
        $arr_prod = $this->db->query("SELECT p.*, l.link FROM product p INNER JOIN link_all l ON p.n_link = l.n_link WHERE 1=1 {$where} {$lm} ")->result_object();
        //var_dump($arr_prod);
        return $arr_prod;
    
    }
    
    public function getDetailsProduct()
    {
        $id = $this->getParamUrl(2);
        //die($id.'adfad');
        return $this->db->query('SELECT * FROM product WHERE id = '.$id)->row_object();
    }
    
    
}